

#include "main.h"
#include "sc1161.h"
#include "ssx1805.h"

encrypt_info encrypt_t;

/**
 * @brief spi_config
 *
 * @return int
 */
int spi_config(void)
{
	uint8_t mode = SPI_MODE;
	uint32_t speed = SPI_SPEED;

	int fd = open(DEV_NAME, O_RDWR);
	if (fd < 0)
	{
		return fd;
	}

	int ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
	if (ret < 0)
	{
		return ret;
	}

	ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
	if (ret < 0)
	{
		return ret;
	}

	return fd;
}

int transfer(char   * tx,char* rx,unsigned int length)
{
    struct spi_ioc_transfer spi_transfer;
    int ret;
	int spi_fd = -1;
	
	spi_fd = open(DEV_NAME, O_RDWR);
	if(spi_fd < 0)
	{
		close(spi_fd);
		return FALSE;
	}

    memset(&spi_transfer, 0, sizeof(struct spi_ioc_transfer));

    spi_transfer.tx_buf = (unsigned long)tx;
    spi_transfer.rx_buf = (unsigned long)rx;
    spi_transfer.len = length;
    spi_transfer.speed_hz = SPI_SPEED_HZ;
    spi_transfer.bits_per_word = SPI_BIT_PER_WORD;
    spi_transfer.delay_usecs = SPI_DELAY_USECS;
    
    ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &spi_transfer);
    if (ret < 0) {
		close(spi_fd);
        return FALSE;
    }
	
	close(spi_fd);
    return TRUE;
}
/** 
 	@retval : enum encrypt_type  
 */
static int encrypt_prob(void)
{
    encrypt_type encrypt_type_sel = Invaild_type;
	char chiptype[32] = {0};

	encrypt_type_sel = sc1161_prob();
	if(encrypt_type_sel == sc1161y)
	{
		goto return_type;
	}

return_type:
	return encrypt_type_sel;
}

/**
  * @brief  获取芯片类型
  * @param  p_client mqtt客户端
  * @param  topic 要订阅的主题
  * @param  qos
  * @retval  1 : 是sc1161y -1 :不是sc1161y
  */
static int is_sc1161y_chip_type(void)
{   
	encrypt_type encrypt_chip = encrypt_prob();
	if(encrypt_chip ==	Invaild_type || encrypt_chip ==	encrypt_type_max)
	{
		 return -1;
	}
	
	switch(encrypt_chip)
	{
		case sc1161y:
			return 1;
		default:
			return -1;
	}
	return -1;

}

static int shell_popen_r(const char * cmd, char *buff, int buff_len)
{
    FILE * fp;
    int ret;
    int i = 0;
    char temp[1024];

    if (!cmd) 
	{
        return -1;
    }

    if ((!buff) || (0 == buff_len)) 
	{
        return -1;
    }

    if ((fp = popen(cmd, "r")) == NULL) 
	{
        return -1;
    } 
	else 
	{
        memset(temp, 0x00, sizeof(temp));
        while (fgets(temp, sizeof(temp), fp)) 
		{
            if ((i + strlen(temp)) >= buff_len) 
			{
                break;
            }
            memcpy(&buff[i], temp, strlen(temp));
            i += strlen(temp);
            memset(temp, 0x00, sizeof(temp));
        }

        if ((ret = pclose(fp)) == -1) 
		{
            return -1;
        } 
		else 
		{
            if (WIFEXITED(ret)) 
			{
                if (0 == WEXITSTATUS(ret)) 
				{
                    ret = 0;
                } 
				else 
				{
                    ret = -1;
                }
            } 
			else if (WIFSIGNALED(ret)) 
			{
                ret = -1;
            } 
			else if (WIFSTOPPED(ret)) 
			{
                ret = -1;
            } 
			else 
			{
                ret = -1;
            }
        }
    }

    return ret;
}

/**
 * @brief check SecurityProxy is installed
 * @param null
 * @return status：SecurityProxy install
 */

int check_SecurityProxy_is_installed(void)
{
	char buff[512] = {0};

	if (!shell_popen_r(CMD_APPM_L, buff, strlen(buff)))
	{
		if (strstr(buff, STR_SECURITYPROXY))
		{
			return 1;
		}
	}
	return 0;
	
}

/**
 * @brief Get the esn object
 *
 * @param put_buffer
 * @return uint8_t
 */
uint8_t get_esn(char *put_buffer)
{
	char pesn[64] = {0};
	char encrypt_sn[32] = {0};
	int chipType = 0;
	int i = 0;

	// /* 检查固件所用系统 */
	// if (!check_firmware_version(deviceConfig.version))
	// {
	// 	// 网关系统是非D9
	// 	chipType = -1;  
	// } 
	// else if (check_SecurityProxy_is_installed())
	// {
	// 	/* 加密芯片代理app已安装 */
	// 	// for (; i < 3; i++) {
	// 	// 	if (!mqtt_init())
	// 	// 		break;
	// 	// }

	// 	if (!strcmp(encrypt_t.chiptype, "1"))
	// 	{
	// 		/* "ED"表示国网 */
	// 		pesn[0] = 'E';
	// 		pesn[1] = 'D';
	// 	}
	// 	else if (!strcmp(encrypt_t.chiptype, "2"))
	// 	{
	// 		/* "EC"表示南网 */
	// 		pesn[0] = 'E';
	// 		pesn[1] = 'C';
	// 	}

	// 	if (encrypt_t.sn != NULL)
	// 	{
	// 		memcpy(pesn + 2, encrypt_t.sn, 16);
	// 		goto back;
	// 	}	
	// }
	// else
	// {
	// 	//直接访问加密芯片，获取类型
	// 	chipType = is_sc1161y_chip_type();
	// }

	chipType = is_sc1161y_chip_type();

	if (chipType == 1)
	{
		/* "ED"表示国网 */
		pesn[0] = 'E';
		pesn[1] = 'D';

		get_sc1161_serial_number(encrypt_sn);
		if (encrypt_sn != NULL)
		{
			strcpy(pesn + 2, (char *)encrypt_sn);
			goto back;
		}
	}
	else
	{
		/* "EC"表示南网 */
		pesn[0] = 'E';
		pesn[1] = 'C';

		get_ssx1805_serial_number(encrypt_sn);
		if (encrypt_sn != NULL)
		{
			memcpy(pesn + 2, (char *)encrypt_sn, 16);   //仅取前8个字节
			goto back;
		}
	}

back:
	sprintf(put_buffer, "ESN: %s\n", pesn);
	return TRUE;
}